package com.lcx.movie.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.Seats;
import com.lcx.movie.service.SeatsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author 李丽君
 * @version 1.0
 * @Description 座位控制类
 * @date 2025/2/10 11:34
 */
@CrossOrigin(origins = "*")
@RestController
public class SeatsController {
    @Autowired
    private SeatsService seatsService;
    /**
     * 根据影厅id查询所有的座位
     * @param hallId 影厅id
     * @return 座位集合
     */
    @GetMapping("/selectSeatsByHallId")
    public Rep selectSeatsByHallId(Integer hallId){
        List<Seats> seatsList = seatsService.selectSeatsByHallId(hallId);
        return new Rep().addData("seatsList", seatsList);
    }

    /**
     *新增座位信息
     * @param map 座位集合数据
     * @return rep对象
     */
    @PostMapping("/saveHallsSeats")
    public Rep saveHallsSeats(@RequestBody Map<String,Object> map){
        int hallId=map.get("hallId")==null?0:Integer.parseInt(map.get("hallId").toString());//影厅id
        // 在新增前先清除查到的数据
        QueryWrapper<Seats> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hall_id",hallId);
        boolean remove = this.seatsService.remove(queryWrapper);
        System.out.println("是否清除成功:"+remove);
        List<Seats> seatsList = (List<Seats>) map.get("seatsList");//影厅的座位集合数据
        // 批量新增座位
        this.seatsService.saveBatch(seatsList);
        System.out.println(seatsList);
        return new Rep();
    }
    //删除座位
    /**
     * 处理删除座位的请求
     * 该方法使用POST请求来删除指定ID的座位信息
     *
     * @param seatId 座位的唯一标识符
     * @return 返回一个包含操作结果状态码的Rep对象
     *         如果删除成功，状态码为200；如果删除失败，状态码为500
     */
    @PostMapping("/deleteSeats")
    public Rep deleteSeats(Integer seatId){
        // 尝试根据座位ID删除座位信息
        boolean i = seatsService.removeById(seatId);
        // 如果删除成功
        if(i){
            // 返回成功状态码200
            return new Rep().setCode(200);
        }
        // 如果删除失败，返回错误状态码500
        return new Rep().setCode(500);
    }

    /**
     * 检查当前影厅是否有座位信息
     * @param hallId 影厅id
     * @return 受影响的行数
     */
    @GetMapping("/getSeatsByHallId")
    public String getSeatsByHallId(@Param("hallId") int hallId){
        int seatsByHallId = this.seatsService.getSeatsByHallId(hallId);
        System.out.println("seatsByHallId = " + seatsByHallId);
        return seatsByHallId>0?"有座位":"无座位";
    }
}
